﻿/********************************************************************************/
// <copyright file="DatabaseTestBase.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
//
// <history>
//  <change who="Hieu Nguyen" date="11/12/2012 5:52:49 PM">Created</change>
// <history>
/********************************************************************************/

namespace JLL.China.Test
{
    using System.Data.Linq;
    using System.Transactions;
    using JLL.China.Infrastructure;
    using JLL.China.Infrastructure.Repositories;
    using JLL.China.Infrastructure.Repositories.LINQ2SQL;
    using Microsoft.Practices.Unity;
    using NUnit.Framework;

    public abstract class DatabaseTestBase
    {
        #region -- Private fields --
        private TransactionScope transaction;
        protected UnityContainer Container { get; set; }
        #endregion

        #region -- Setup and Teardown functions --

        [TestFixtureSetUp]
        public virtual void OnTestFixtureSetUp()
        {
            Container = new UnityContainer();
            Container.RegisterType<DataContext,JLLCHINADataContext >(new PerThreadLifetimeManager(), new InjectionConstructor()); // For mailer service
            ObjectFactory.Container = Container;
        }

        [SetUp]
        public void OnTestSetup()
        {
             transaction = new TransactionScope();
            TestSetup();
        }

        [TearDown]
        public void OnTestTearDown()
        {
            if (transaction != null)
            {
                transaction.Dispose();
                transaction = null;
            }
            TestTearDown();
        }

        [TestFixtureTearDown]
        public virtual void OnTestFixtureTearDown()
        {
            Container.Dispose();
            OnTestTearDown();
        }

        protected virtual void TestSetup()
        {
        }

        protected virtual void TestTearDown()
        {
        }
        #endregion
    }
}
